home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / bin2arr.zip / SOURCE.ZIP / BIN2ARR.CPP next >
C/C++ Source or Header  |  1994-12-27  |  4KB  |  165 lines

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<io.h>
  5. #include<fcntl.h>
  6.  
  7. coolprintf(char *string,char FIRST_COLOR = 8,char SECOND_COLOR=7,char REST_COLOR=15)
  8. {
  9.       register int stringloop;
  10.       char first_letter;
  11.  
  12.  
  13.       textcolor(FIRST_COLOR);
  14.       cprintf("%c",string[0]);
  15.       textcolor(SECOND_COLOR);
  16.       cprintf("%c",string[1]);
  17.       textcolor(REST_COLOR);
  18.       stringloop=1;//2
  19.      while(string[stringloop++] != NULL)
  20.      {
  21.               if (first_letter==1)
  22.               {
  23.                      textcolor(FIRST_COLOR);
  24.                      cprintf("%c",string[stringloop]);
  25.                       textcolor(SECOND_COLOR);
  26.                       cprintf("%c",string[stringloop+1]);
  27.                       stringloop++;
  28.                       first_letter=0;
  29.                       textcolor(REST_COLOR);
  30.                }
  31.                else
  32.                cprintf("%c",string[stringloop]);
  33.                if (string[stringloop]==32) first_letter=1;
  34.        }
  35.  
  36. return 0;
  37. }
  38.  
  39. char coolgets(char *string,char FIRST_COLOR = 2,char REST_COLOR=10)
  40. {
  41.       
  42.      unsigned char c;
  43.       int count=1;
  44.       string[0]=32;
  45.       char space_bar=0;
  46.  
  47.       while ((c = getch()) != 13 )
  48.       {
  49.               if(c==8)
  50.             {
  51.                    string[count-2]=32;
  52.                    if (count>1) cprintf("%c%c%c",8,32,8);
  53.                    else space_bar=0;
  54.                    count--;
  55.                    if (count<1) count =1;
  56.                     //if (string[count-2]==32) space_bar=0;
  57.                       if (string[count-2]==32) space_bar=0;
  58.                       if (count <1) if (string[count-2]==32) space_bar=0;
  59.                       else space_bar=1;
  60.               }
  61.  
  62.               if (c>32 && c<128 || c == 32)
  63.               {
  64.                      if (count <50)
  65.                      {
  66.                           count++;
  67.                           if (space_bar==0) {textcolor(FIRST_COLOR); space_bar=1;}
  68.                           else textcolor(REST_COLOR);
  69.                           if (c>32 && c<65) textcolor(REST_COLOR);
  70.                             if (c>47 && c<60) textcolor(FIRST_COLOR);
  71.  
  72.                           string[count-2]=c;
  73.                           cprintf("%c", c);
  74.                           space_bar=0;
  75.                           if (c==32) space_bar=0;
  76.                           else space_bar=1;
  77.                      }
  78.               }
  79.       }
  80.       printf("\n");
  81.       string[count-1]=NULL;
  82. return 0;
  83. }
  84.  
  85. main(int argc,unsigned char* argv[])
  86. {
  87.  
  88. unsigned char buf[10];
  89. unsigned char array_name[80];
  90. unsigned long count,fl,count2=0,line=0;
  91. unsigned char string[80];
  92.  
  93. int handle;
  94. FILE *fp;
  95.  
  96. if (argc<3)
  97.     {
  98.  
  99.     printf("\n");
  100.     coolprintf("Usage:");
  101.     printf("\n");
  102.     
  103.     coolprintf("BIN2ASM [SOURCE BINARY] [OUTPUT C INCLUDE FILE]");
  104.     printf("\n");
  105.     
  106.     exit(1);
  107.     }
  108.  
  109. if ((handle = open(argv[1], O_RDONLY | O_BINARY)) == -1)
  110. {
  111.     printf("\n");
  112.     sprintf(string,"Error: Cannot Open '%s'",argv[1]);
  113.     coolprintf(string);
  114.     printf("\n");
  115.     exit(1);
  116. }
  117.  
  118. if((fp = fopen(argv[2], "w+")) ==NULL)
  119. {
  120.     printf("\n");
  121.     sprintf(string,"Error: Cannot Create '%s'",argv[2]);
  122.     coolprintf(string);
  123.     printf("\n");
  124.     exit(1);
  125. }
  126.  
  127. fl=filelength(handle);
  128.  
  129. printf("\n");
  130. coolprintf("Binary to C Array Converter By EMiNENT DOOM");
  131. printf("\n\n");
  132. coolprintf("Input Array Name:");
  133.  
  134. coolgets(array_name);
  135. printf("\r                      ");
  136.  
  137. fprintf(fp,"unsigned char %s[]= {\n",array_name);
  138.  
  139. for(count=0;count<=fl;count++)
  140. {
  141. read(handle, buf, 1);
  142.  
  143. if(count<fl) fprintf(fp,"%3d,",buf[0]);
  144. if(count==fl) fprintf(fp,"%3d",buf[0]);
  145.  
  146. if(count2++ >=18) 
  147.     {
  148.     count2=0;
  149.     fprintf(fp,"\n");
  150.  
  151.     printf("\r");
  152.     sprintf(string,"Line: %d",line);
  153.     coolprintf(string);
  154.     line++;
  155.     }
  156. }
  157. fprintf(fp,"\n};");
  158. printf("\n");
  159.  
  160. fclose(fp);
  161. close(handle);                    /* close the file */
  162.  
  163. return 0;
  164. }
  165.